// 订单入库参数处理
export const orderInSqlParams = (res: any) => {
  const val: any = {
    ...res,
  };
  val.gateId = val?.id;
  val.gateUser = val?.user;
  val.userId = val?.text?.split('-')[2];
  delete val?.id;
  delete val?.create_time;
  delete val?.update_time;
  return val;
};

// 订单数据-mock
export const mockdata = {
  status: 'open',
  size: 1,
  left: 1,
  id: 36028797331937610,
  is_liq: false,
  is_close: false,
  contract: 'BTC_USDT',
  text: 't-hd-2',
  fill_price: '0',
  iceberg: 0,
  tif: 'gtc',
  is_reduce_only: false,
  create_time: 1738508981.059,
  update_time: 1738508981.059,
  price: '97000',
  mkfr: '0.00015',
  tkfr: '0.00038',
  refr: '0',
  refu: 0,
  user: 19759369,
  biz_info: 'ch:gateapinode',
  amend_text: '-',
  stp_act: '-',
  stp_id: 0,
  update_id: 1,
  pnl: '0',
  pnl_margin: '0',
};

// 处理仓位的止盈损数据
export const handlePositionZhiYingSunData = (zhiyingRes: any, isZuoDuo: boolean) => {
  const arr = zhiyingRes;
  const zhiYingSunData = {
    quanCang: {
      zhiYing: null,
      zhiSun: null,
    },
    buFenCang: {
      list: [],
      max: null,
      min: null,
    },
  };
  // 全仓
  const quanCangList: any = arr.filter((item: any) => item?.initial?.size === 0);
  if (quanCangList.length > 0) {
    quanCangList.forEach((item: any) => {
      const { rule } = item?.trigger;
      if (isZuoDuo) {
        if (rule === 1) zhiYingSunData.quanCang.zhiYing = item;
        if (rule === 2) zhiYingSunData.quanCang.zhiSun = item;
      } else {
        if (rule === 2) zhiYingSunData.quanCang.zhiYing = item;
        if (rule === 1) zhiYingSunData.quanCang.zhiSun = item;
      }
    });
  }
  // 部分仓
  const bfList = arr.filter((item: any) => item?.initial?.size !== 0);
  zhiYingSunData.buFenCang.list = bfList;
  const minMax = handelBufenZhiYingSun(bfList, isZuoDuo);
  zhiYingSunData.buFenCang.max = minMax.max;
  zhiYingSunData.buFenCang.min = minMax.min;
  return zhiYingSunData;
};

// 部分仓位区分止盈止损，找到最小止盈和最大止损
function handelBufenZhiYingSun(list: any, isZuoDuo: boolean) {
  const obj: any = {
    max: null,
    min: null,
  };
  if (list.length === 0) return obj;
  const zhiyings = list?.filter((item: any) => item?.trigger.rule === (isZuoDuo ? 1 : 2));
  const zhisuns = list?.filter((item: any) => item?.trigger.rule === (isZuoDuo ? 2 : 1));
  if (zhiyings.length > 0) obj.max = Math.max(...zhiyings.map((item: any) => Number(item?.trigger?.price)));
  if (zhisuns.length > 0) obj.min = Math.min(...zhisuns.map((item: any) => Number(item?.trigger.price)));
  return obj;
}
